package com.gitee.huanminabc.db_freedom.assist.impl;

import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.gitee.huanminabc.db_freedom.assist.FreedomSqlRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class FreedomSqlRunnerImpl implements FreedomSqlRunner {
    private Class<?> clazz;

    public FreedomSqlRunnerImpl() {
    }

    public FreedomSqlRunnerImpl(Class<?> clazz) {
        this.clazz = clazz;
    }
    @Override
    public List<Map<String, Object>> selectList(String sql, Object... args) {
        return Collections.emptyList();
    }

    private SqlSession sqlSession() {
        return SqlSessionUtils.getSqlSession(getSqlSessionFactory());
    }

    private SqlSessionFactory getSqlSessionFactory() {
        return Optional.ofNullable(clazz).map(GlobalConfigUtils::currentSessionFactory).orElse(SqlHelper.FACTORY);
    }
}
